<?php
namespace bee;
/**
 *
 * @param $text
 * @param $level
 * @param $log_ref
 * @return int
 */
function toLog($text, $level = 0, $log_ref = '')
{	//global $sys_LOG_FILE_PATH;
  $cfg = config::getInstance();
	global $client;
	if($cfg->sys_LOG_FILE_PATH)
	{	$arr_src = array("\n", "\r");
		$arr_rpl = array("*n*", "*r*");
		$timestamp = date('Y-m-d H:i:s.u');

		if($dat = fopen($cfg->sys_LOG_FILE_PATH . "/" . date('Y-m-d') . '_log.txt', 'a'))
		{	$str = sprintf( "%s *%s* %d %s (%s)", $timestamp, $client, $level, $text, $log_ref ? date('Y-m-d H:i:s.u', (double)$log_ref) : '');
			fputs($dat, str_replace($arr_src, $arr_rpl, $str) . "\r\n");
			fclose($dat);
			return microtime(true);
		}	elseif($dat = fopen($cfg->sys_LOG_FILE_PATH . microtime(true) . '_log.txt', 'a'))
		{	$str = sprintf( "%s %s %d %s (%s)", $timestamp, $client, $level, $text, $log_ref ? date('Y-m-d H:i:s.u', (double)$log_ref) : '');
			fputs($dat, str_replace($arr_src, $arr_rpl, $str) . "\r\n");
			fclose($dat);
			return microtime(true);
		}	else
		{	return 0;
		}
	}

}

class log extends singleton
{ protected static $__instance;
  protected static $logTimes;
  static function toLog($text, $level = 0, $log_ref = '')
  { $cfg = config::getInstance();
    return;
    global $client;
    if($cfg->sys_LOG_FILE_PATH)
    { $arr_src = array("\n", "\r");
      $arr_rpl = array("*n*", "*r*");
      $timestamp = date('Y-m-d H:i:s.u');

      if($dat = fopen($cfg->sys_LOG_FILE_PATH . "/" . date('Y-m-d') . '_log.txt', 'a'))
      { $str = sprintf( "%s *%s* %d %s (%s)", $timestamp, $client, $level, $text, $log_ref ? date('Y-m-d H:i:s.u', (double)$log_ref) : '');
        fputs($dat, str_replace($arr_src, $arr_rpl, $str) . "\r\n");
        fclose($dat);
        $return =  microtime(true);
        static::$logTimes[] = $return;
        return $return;
      } elseif($dat = fopen($cfg->sys_LOG_FILE_PATH . microtime(true) . '_log.txt', 'a'))
      { $str = sprintf( "%s %s %d %s (%s)", $timestamp, $client, $level, $text, $log_ref ? date('Y-m-d H:i:s.u', (double)$log_ref) : '');
        fputs($dat, str_replace($arr_src, $arr_rpl, $str) . "\r\n");
        fclose($dat);

        $return =  microtime(true);
        static::$logTimes[] = $return;
        return $return;
      } else
      { return 0;
      }
    }
  }
  static function getLogTimes()
  { return static::$logTimes;
  }
}



